package com.kmvc.message.activity;

import java.util.List;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.kmvc.message.R;
import com.kmvc.message.encryption.Encryption;
import com.kmvc.message.lock.LockPatternUtils;
import com.kmvc.message.lock.LockPatternView;
import com.kmvc.message.lock.LockPatternView.Cell;
import com.kmvc.message.lock.LockPatternView.DisplayMode;
import com.kmvc.message.lock.LockPatternView.OnPatternListener;
import com.kmvc.message.service.MessageService;
import com.kmvc.message.tools.WebService;
import com.kmvc.ui.IphoneTitleBarActivity;

public class GesturePWDCreateActivity extends IphoneTitleBarActivity implements
		OnClickListener {

	private LockPatternView lockview;
	private SharedPreferences shp;
	private String old_pwd, pwd = "", result1 = "";;
	private int count = 0, wrongcount = 4,time=0;
	private Handler handlers = new Handler();
	private Handler handler1 = new Handler();
	private ProgressDialog progress;
	
	private String PhoneNumber;

	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.gesturepassword_create);
		setTitle(R.string.gesture_password_setting);
		setLeftButton(R.string.dialog_cancel, null);

		lockview = (LockPatternView) findViewById(R.id.gesturepwd_create_lockview);
		PhoneNumber=getIntent().getStringExtra("PhoneNumber");
		if(PhoneNumber!=null&&!PhoneNumber.equals(""))
		{
			shp=getSharedPreferences("lock_pwd", 0);
			Toast.makeText(GesturePWDCreateActivity.this, "请输入新密码!",
					Toast.LENGTH_SHORT).show();
		}
		else 
		{
			Toast.makeText(GesturePWDCreateActivity.this, "请先绘制原密码!",
					Toast.LENGTH_SHORT).show();
		}
		

		lockview.setOnPatternListener(new OnPatternListener() {

			@Override
			public void onPatternStart() {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPatternCleared() {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPatternCellAdded(List<Cell> pattern) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPatternDetected(List<Cell> pattern) {
						
					shp = getSharedPreferences("lock_pwd", 0);
					if(PhoneNumber!=null&&!PhoneNumber.equals("")&&count==0)
					{
						count=1;
						time=0;
						old_pwd = LockPatternUtils.patternToString(pattern);
					}
					else  if(PhoneNumber==null||PhoneNumber.equals(""))
					{
						old_pwd=shp.getString("lock_pwd", "");
					}
					if (old_pwd.equals("")) {
						Toast.makeText(GesturePWDCreateActivity.this,
								"数据读取出错，请重新输入原密码!", Toast.LENGTH_SHORT).show();
					} 
					else 
					{
						if (count == 0) 
						{
							if (old_pwd.equals(Encryption.encode(LockPatternUtils
									.patternToString(pattern)))) {
								Toast.makeText(GesturePWDCreateActivity.this,
										"密码验证成功，请输入新密码!", Toast.LENGTH_SHORT).show();
								count = 1;
							} 
							else 
							{
								lockview.setDisplayMode(DisplayMode.Wrong);
								wrongcount--;
								if (0 < wrongcount & wrongcount < 4) {
									Toast.makeText(
											GesturePWDCreateActivity.this,
											"您还有" + wrongcount + "次机会,"
													+ wrongcount + "次机会用完后,将退出系统!",
											Toast.LENGTH_SHORT).show();
								}
								if (wrongcount < 1) {
									Toast.makeText(GesturePWDCreateActivity.this,
											"抱歉,您的机会用完了,五秒后将退出系统!",
											Toast.LENGTH_SHORT).show();
									handlers.postDelayed(run, 5000);
								}
							}
						}
						else{
							if(count==1){
								if(!old_pwd.equals(Encryption.encode(LockPatternUtils
										.patternToString(pattern)))){
									if(time==0){
										pwd = LockPatternUtils.patternToString(pattern);
										time++;
										Toast.makeText(GesturePWDCreateActivity.this, "请再输入一次新密码!",
												Toast.LENGTH_SHORT).show();
										
										DrawSmall(pwd.replace("[", "").replace("]", ""));
									}else if(time==1 & pwd.equals(LockPatternUtils
										.patternToString(pattern))){
										TelephonyManager mTelephonyMgr = (TelephonyManager) GesturePWDCreateActivity.this
												.getSystemService(Context.TELEPHONY_SERVICE);
										String IMSI = mTelephonyMgr.getSubscriberId();
										postpwdtoservice1(IMSI, pwd);
									}
								}else{
									Toast.makeText(GesturePWDCreateActivity.this, "请输入与旧密码不同的密码!",
											Toast.LENGTH_SHORT).show();
								}
							}
						}
					}
				}
			//}
		});
	}

	Runnable run = new Runnable() {

		@Override
		public void run() {
			System.exit(0);
		}
	};

	protected void postpwdtoservice1(final String IMSI, final String a) {
		progress = ProgressDialog.show(this, "请稍候", "密码更新中...", true);
		progress.show();
		new Thread() {
			public void run() {
				result1 = "" + WebService.Savepwd(IMSI, a);
				handler1.post(r1);
			}
		}.start();
	}

	Runnable r1 = new Runnable() {

		public void run() {
			if (result1.equals("1")) {
				// Toast.makeText(LockMainActivity.this, "密码设置成功",
				// Toast.LENGTH_SHORT).show();
				Editor editor = shp.edit();
				editor.putString("lock_pwd", Encryption.encode(pwd));
				editor.commit();
				progress.dismiss();
				AlertDialog.Builder builder = new AlertDialog.Builder(
						GesturePWDCreateActivity.this);
				builder.setCancelable(false);
				builder.setMessage("密码修改成功\n您的新密码为："
						+ pwd.replace(",", "").replace("[", "")
								.replace("]", "") + "请牢记");
				builder.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								Intent intserv = new Intent(
										GesturePWDCreateActivity.this,
										MessageService.class);
								intserv.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
								GesturePWDCreateActivity.this
										.startService(intserv);
								Intent intent = new Intent(
										GesturePWDCreateActivity.this,
										MainTab.class);
								GesturePWDCreateActivity.this
										.startActivity(intent);
								GesturePWDCreateActivity.this.finish();
								overridePendingTransition(R.anim.slide_left_in,
										R.anim.slide_left_out);
							}
						}).create();
				if(PhoneNumber!=null&&!PhoneNumber.equals(""))
				{
					Intent intent=new Intent(GesturePWDCreateActivity.this, MainTab.class);
					startActivity(intent);
				}	
			 GesturePWDCreateActivity.this.finish();
			 Toast.makeText(GesturePWDCreateActivity.this, "密码修改成功！",
						Toast.LENGTH_SHORT).show();
			} else {
				progress.dismiss();
				Toast.makeText(GesturePWDCreateActivity.this, "密码修改失败,请检查网络",
						Toast.LENGTH_SHORT).show();
			}
		}

	};

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}
	
	
	//绘制上面图形的
	public void DrawSmall(String IDS)
	{
		View gesturepwd_setting_preview_0 =findViewById(R.id.gesturepwd_setting_preview_0);
		View gesturepwd_setting_preview_1 =findViewById(R.id.gesturepwd_setting_preview_1);
		View gesturepwd_setting_preview_2 =findViewById(R.id.gesturepwd_setting_preview_2);
		View gesturepwd_setting_preview_3 =findViewById(R.id.gesturepwd_setting_preview_3);
		View gesturepwd_setting_preview_4 =findViewById(R.id.gesturepwd_setting_preview_4);
		View gesturepwd_setting_preview_5 =findViewById(R.id.gesturepwd_setting_preview_5);
		View gesturepwd_setting_preview_6 =findViewById(R.id.gesturepwd_setting_preview_6);
		View gesturepwd_setting_preview_7 =findViewById(R.id.gesturepwd_setting_preview_7);
		View gesturepwd_setting_preview_8 =findViewById(R.id.gesturepwd_setting_preview_8);
		

		gesturepwd_setting_preview_0.setBackgroundResource(R.drawable.qq_gesture_create_grid_normal);
		gesturepwd_setting_preview_1.setBackgroundResource(R.drawable.qq_gesture_create_grid_normal);
		gesturepwd_setting_preview_2.setBackgroundResource(R.drawable.qq_gesture_create_grid_normal);
		gesturepwd_setting_preview_3.setBackgroundResource(R.drawable.qq_gesture_create_grid_normal);
		gesturepwd_setting_preview_4.setBackgroundResource(R.drawable.qq_gesture_create_grid_normal);
		gesturepwd_setting_preview_5.setBackgroundResource(R.drawable.qq_gesture_create_grid_normal);
		gesturepwd_setting_preview_6.setBackgroundResource(R.drawable.qq_gesture_create_grid_normal);
		gesturepwd_setting_preview_7.setBackgroundResource(R.drawable.qq_gesture_create_grid_normal);
		gesturepwd_setting_preview_8.setBackgroundResource(R.drawable.qq_gesture_create_grid_normal);
		
		String[] arr=IDS.split(",");
		if(arr.length>0)
		{
			for(int i=0;i<arr.length;i++)
			{
				int idString=Integer.parseInt(arr[i].trim()) ;
				switch (idString) 
				{
					case 0:
						gesturepwd_setting_preview_0.setBackgroundResource(R.drawable.qq_gesture_create_grid_selected);
						break;
					case 1:
						gesturepwd_setting_preview_1.setBackgroundResource(R.drawable.qq_gesture_create_grid_selected);
						break;
					case 2:
						gesturepwd_setting_preview_2.setBackgroundResource(R.drawable.qq_gesture_create_grid_selected);
						break;
					case 3:
						gesturepwd_setting_preview_3.setBackgroundResource(R.drawable.qq_gesture_create_grid_selected);
						break;
					case 4:
						gesturepwd_setting_preview_4.setBackgroundResource(R.drawable.qq_gesture_create_grid_selected);
						break;
					case 5:
						gesturepwd_setting_preview_5.setBackgroundResource(R.drawable.qq_gesture_create_grid_selected);
						break;
					case 6:
						gesturepwd_setting_preview_6.setBackgroundResource(R.drawable.qq_gesture_create_grid_selected);
						break;
					case 7:
						gesturepwd_setting_preview_7.setBackgroundResource(R.drawable.qq_gesture_create_grid_selected);
						break;
					case 8:
						gesturepwd_setting_preview_8.setBackgroundResource(R.drawable.qq_gesture_create_grid_selected);
						break;

					default:
						break;
				}
			}
		}
		
		
	}
	
	
}
